package com.wlc.service.impl;

import com.wlc.mapper.EmployeeMapper;
import com.wlc.pojo.Employee;
import com.wlc.pojo.PageResult;
import com.wlc.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public PageResult<Employee> findEmployeeList(String name, Long deptId, Integer workState, Integer page, Integer size) {
        log.info("分页查询员工列表: name={}, deptId={}, workState={}, page={}, size={}",
                name, deptId, workState, page, size);

        // 参数校验和默认值
        if (page == null || page < 1) page = 1;
        if (size == null || size < 1) size = 10;
        if (size > 100) size = 100;

        // 查询总数
        Long total = employeeMapper.selectEmployeeCount(name, deptId, workState);

        // 查询分页数据
        List<Employee> employees = employeeMapper.selectEmployeeList(name, deptId, workState, (page - 1) * size, size);

        return new PageResult<>(total, employees);
    }

    @Override
    public Employee findById(Long id) {
        log.info("根据ID查询员工: {}", id);
        return employeeMapper.selectById(id);
    }

    @Override
    public List<Employee> findAll() {
        log.info("查询所有员工");
        return employeeMapper.selectAll();
    }

    @Override
    public List<Employee> findByDeptId(Long deptId) {
        log.info("根据部门ID查询员工: deptId={}", deptId);
        return employeeMapper.selectByDeptId(deptId);
    }

    @Override
    @Transactional
    public void save(Employee employee) {
        log.info("新增员工: {}", employee.getName());

        // 校验身份证号是否已存在
        Employee existingEmployee = employeeMapper.selectByIdNumber(employee.getIdNumber());
        if (existingEmployee != null) {
            throw new RuntimeException("身份证号已存在");
        }

        // 设置默认值
        if (employee.getWorkState() == null) {
            employee.setWorkState(1); // 默认在职
        }
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        int result = employeeMapper.insert(employee);
        if (result <= 0) {
            throw new RuntimeException("新增员工失败");
        }

        log.info("新增员工成功: {}", employee.getName());
    }

    @Override
    @Transactional
    public void update(Employee employee) {
        log.info("更新员工: {}", employee.getId());

        Employee existingEmployee = employeeMapper.selectById(employee.getId());
        if (existingEmployee == null) {
            throw new RuntimeException("员工不存在");
        }

        // 校验身份证号是否被其他员工使用
        if (employee.getIdNumber() != null) {
            Employee employeeByIdNumber = employeeMapper.selectByIdNumber(employee.getIdNumber());
            if (employeeByIdNumber != null && !employeeByIdNumber.getId().equals(employee.getId())) {
                throw new RuntimeException("身份证号已被其他员工使用");
            }
        }

        employee.setUpdateTime(LocalDateTime.now());
        int result = employeeMapper.update(employee);
        if (result <= 0) {
            throw new RuntimeException("更新员工失败");
        }

        log.info("更新员工成功: {}", employee.getId());
    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        log.info("删除员工: {}", id);

        Employee employee = employeeMapper.selectById(id);
        if (employee == null) {
            throw new RuntimeException("员工不存在");
        }

        int result = employeeMapper.deleteById(id);
        if (result <= 0) {
            throw new RuntimeException("删除员工失败");
        }

        log.info("删除员工成功: {}", id);
    }

    @Override
    @Transactional
    public void updateWorkState(Long id, Integer workState) {
        log.info("更新员工状态: id={}, workState={}", id, workState);

        Employee employee = employeeMapper.selectById(id);
        if (employee == null) {
            throw new RuntimeException("员工不存在");
        }

        if (workState < 1 || workState > 3) {
            throw new RuntimeException("状态值不合法");
        }

        int result = employeeMapper.updateWorkState(id, workState);
        if (result <= 0) {
            throw new RuntimeException("更新员工状态失败");
        }

        log.info("更新员工状态成功: id={}, workState={}", id, workState);
    }
}